﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADONETDataServicesExtension.BLLUtils;
using System.Data.Objects.DataClasses;

namespace BLL
{
    public abstract class BaseEntityType<EFEntityType> : IEntity where EFEntityType : EntityObject
    {
        private Func<EFEntityType> _createEntity;

        public BaseEntityType(Func<EFEntityType> createEntity)
        {
            _createEntity = createEntity;
        }
        public BaseEntityType(EFEntityType efEntity)
        {
            if (efEntity == null)
                throw new InvalidOperationException();
            EFEntity = efEntity;
        }

        private EFEntityType _efEntity;
        protected EFEntityType EFEntity
        {
            get
            {
                if (_efEntity == null)
                {
                    if (_createEntity == null)
                        throw new InvalidOperationException();
                    _efEntity = _createEntity();
                }
                return _efEntity;
            }
            set { _efEntity = value; }
        }

        internal string Login { get; set; }

        EntityObject IEntity.EFEntity
        {
            get { return EFEntity; }
        }
    }
}
